package com.ymatou.iapi.trading.sellerquery2.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.ordersync.service.SyncVerifyService;
import com.ymatou.iapi.trading.sellerquery2.parameter.GetOrderExtraBean;
import com.ymatou.iapi.trading.sellerquery2.parameter.resp.GetOrderExtraResp;
import com.ymatou.iapi.trading.sellerquery2.service.GetOrderExtraCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.PaymentProxyCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 查询支付方式
 */
@TestSuite
public class Ts_GetOrderExtra {

	private static GetOrderExtraBean getOrderExtraBean;
	private static GetOrderExtraCall getOrderExtraCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询支付方式");
	}

	@Before
	public void caseUp() {
		getOrderExtraBean = new GetOrderExtraBean();
		getOrderExtraCall = new GetOrderExtraCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getOrderExtraCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {

		GetOrderExtraResp expectResponse = new GetOrderExtraResp();

		ConstructExpectedResponse(expectResponse);

		GetOrderExtraResp actualResponse = getOrderExtraCall
				.getOrderExtraResponse();

		DataUtil.verifyDecimalEquals(expectResponse.getPaid3Part(),
				actualResponse.getPaid3Part(), "paid3Part");
		DataUtil.verifyDecimalEquals(expectResponse.getPaidAccount(),
				actualResponse.getPaidAccount(), "paidAccount");

		DataUtil.verifyEquals(expectResponse.getPayNo(),
				actualResponse.getPayNo(), "payNo");

		for (int i = 0; i < expectResponse.getRelatedId().size(); i++) {

			DataUtil.verifyEquals(expectResponse.getRelatedId().get(i),
					actualResponse.getRelatedId().get(i), "relatedId" + (i + 1));

		}

	}
	private void ConstructExpectedResponse(GetOrderExtraResp response) {

		List<Map> results = execute1();

		if (results.size() < 1)

			return;

		int iMainOrderId = (Integer) results.get(0).get("iMainOrderId");

		List<Map> results2 = execute2(iMainOrderId);

		ConvertResult(response, results, results2);
	}
	private void ConvertResult(GetOrderExtraResp response, List<Map> results1,
			List<Map> results2) {

		if (results1.size() > 0) {
			DataUtil.mapToBean(response, results1.get(0));
		}

		List<Integer> relatedId = new ArrayList<Integer>();
		for (Map m : results2) {
			relatedId.add((Integer) m.get("iorderid"));
		}

		response.setRelatedId(relatedId);

	}
	private List<Map> execute1() {

		String sqlString = "SELECT  o.iMainOrderId ,t.sTradeNo as payNo ,"
				+ "s.fPaidAmountOfThirdParty as paid3Part,s.fPaidAmountOfYmtAccount as paidAccount"
				+ " FROM dbo.ymt_orders o " + " JOIN dbo.Ymt_TradingInfo t "
				+ " ON o.iTradingId = t.iTradingId "
				+ " join dbo.ymt_orderstate s " + " on o.iorderid=s.iOrderId "
				+ " where o.ibuyerid= " + getOrderExtraBean.getSellerId()
				+ " and o.iorderid =" + getOrderExtraBean.getOrderId();

		return new DefaultWapper().executeSql(sqlString);
	}

	private List<Map> execute2(int iMainOrderId) {

		String sqlString = "select o.iorderid from ymt_orders o where iMainOrderId="
				+ iMainOrderId;

		return new DefaultWapper().executeSql(sqlString);
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderExtra_001() {
		Logger.start(true, "查询支付方式成功");
		try {
			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder3();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 混合支付付款

			BigDecimal blanceAmout = new BigDecimal(new Random().nextInt(50));

			String payType = "13";
			int thirdTradingId = orderCallService.orderPayRequest(userId,
					blanceAmout, tradingId, payType);

			// 支付网关支付回调交易
			PaymentProxyCallService.notifyCall(payType,
					String.valueOf(thirdTradingId),
					totalPrice.subtract(blanceAmout).toString(), userId);

			Thread.sleep(1000);

			SyncVerifyService.getSyncResult(orderId);

			getOrderExtraBean.setOrderId(orderId);
			getOrderExtraBean.setSellerId(sellerId);
			getOrderExtraBean.setType(1);

			// 发送请求

			getOrderExtraCall.setData(getOrderExtraBean);
			getOrderExtraCall.callService();

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetOrderExtra_002() {
		Logger.start(true, "卖家不存在,查询结果为空");
		try {
			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder3();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			getOrderExtraBean.setOrderId(orderId);
			getOrderExtraBean.setSellerId(111111);
			getOrderExtraBean.setType(1);

			// 发送请求

			getOrderExtraCall.setData(getOrderExtraBean);
			getOrderExtraCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getOrderExtraCall.getCode(),
					"验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetOrderExtra_003() {
		Logger.start(true, "卖家为空,查询结果为空");
		try {
			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder3();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			getOrderExtraBean.setOrderId(orderId);
			//getOrderExtraBean.setSellerId(111111);
			getOrderExtraBean.setType(1);

			// 发送请求

			getOrderExtraCall.setData(getOrderExtraBean);
			getOrderExtraCall.callService();

			Logger.verifyEquals(ECode.ERROR400, getOrderExtraCall.getCode(),
					"验证Code");

			Logger.verifyEquals(ECode.BCODE8, getOrderExtraCall.getBCode(),
					"验证bcode");

			Logger.verifyEquals("sellerId:卖家id不能为0",
					getOrderExtraCall.getMsg(), "验证msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetOrderExtra_004() {
		Logger.start(true, "订单号不存在,查询结果为空");
		try {
			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder3();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			getOrderExtraBean.setOrderId(11111);
			getOrderExtraBean.setSellerId(3383);
			getOrderExtraBean.setType(1);

			// 发送请求

			getOrderExtraCall.setData(getOrderExtraBean);
			getOrderExtraCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, getOrderExtraCall.getCode(),
					"验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetOrderExtra_005() {
		Logger.start(true, "订单号为空,查询结果为空");
		try {
			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder3();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			//getOrderExtraBean.setOrderId(11111);
			getOrderExtraBean.setSellerId(3383);
			getOrderExtraBean.setType(1);

			// 发送请求

			getOrderExtraCall.setData(getOrderExtraBean);
			getOrderExtraCall.callService();

			Logger.verifyEquals(ECode.ERROR400, getOrderExtraCall.getCode(),
					"验证Code");

			Logger.verifyEquals(ECode.BCODE8, getOrderExtraCall.getBCode(),
					"验证bcode");

			Logger.verifyEquals("orderId:订单号非法", getOrderExtraCall.getMsg(),
					"验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}